package com.pignest.hotel.ui.protocol;

import java.util.Date;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.ProtocolUnit;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class ProtocolWindow extends Window implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -488088330384303192L;

	private Textbox pNo;
	
	private Textbox pUnit;
	
	private Textbox pName;
	
	private Textbox pPhone;
	
	private Checkbox pIsHangUp;
	
	private Textbox pMaxPrice;
	
	private Textbox pRemark;
	
	private String pId;
	
	private ProtocolUnit unit;
	@Override
	public void onEvent(Event arg0) throws Exception {
		// TODO Auto-generated method stub

	}
	public void onCreate() {
		pNo=(Textbox)getFellow("pNo");
		pUnit=(Textbox)getFellow("pUnit");
		pName=(Textbox)getFellow("pName");
		pPhone=(Textbox)getFellow("pPhone");
		pIsHangUp=(Checkbox)getFellow("pIsHangUp");
		pMaxPrice=(Textbox)getFellow("pMaxPrice");
		pRemark=(Textbox)getFellow("pRemark");
		init();
		pIsHangUp.addEventListener(Events.ON_CHECK, new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				if(pIsHangUp.isChecked())
				{
					pMaxPrice.setReadonly(false);
					if(pId!=null){
						pMaxPrice.setValue(Utils.toPrice(unit.getMaxPrice()));
					}
				}else{
					pMaxPrice.setReadonly(true);
					if(pId!=null){
						pMaxPrice.setValue("0");
					}
				}
			}
		});
	}
	public void init()
	{
		if(pId==null)
		{
			pNo.setValue(DateUtil.toString(new Date(), "yyyyMMddHHmmss"));
			
		}else{
			unit =Services.getOtherBusiService().get(ProtocolUnit.class, pId);
			pNo.setValue(unit.getProtocolNo());
			pUnit.setValue(unit.getCompany());
			pName.setValue(unit.getContactName());
			pPhone.setValue(unit.getPhone());
			pIsHangUp.setChecked(unit.isEnableHangUp());
			pMaxPrice.setValue(Utils.toPrice(unit.getMaxPrice()));
			pRemark.setValue(unit.getRemark());
		}
		if(pIsHangUp.isChecked())
		{
			pMaxPrice.setReadonly(false);
		}else
			pMaxPrice.setReadonly(true);
	}
	public void save(){
		String no =pNo.getValue();
		String company =pUnit.getValue();
		String name =pName.getValue();
		String phone =pPhone.getValue();
		String price =pMaxPrice.getValue();
		if(!Utils.checkPrice(price)){
			UIUtils.alert("限额必须为正数!");
			return ;
		}
		int maxPrice =Utils.toPrice(price);
		String remark =pRemark.getValue();

		if(pId==null)
		{
			unit = new ProtocolUnit(no, company, name, phone);
			unit.setMaxPrice(maxPrice);
			unit.setEnableHangUp(pIsHangUp.isChecked());
			unit.setRemark(remark);
			Services.getOtherBusiService().insertProtocol(unit);
			
		}else
		{
			unit.setProtocolNo(no);
			unit.setCompany(company);
			unit.setContactName(name);
			unit.setEnableHangUp(pIsHangUp.isChecked());
			unit.setRemark(remark);
			unit.setMaxPrice(maxPrice);
			unit.setOpTime(new Date());
			unit.setOpUser(Hotel.getCurrentUser().getName());
			Services.getOtherBusiService().update(unit);
		}
		UIUtils.alert("操作成功!");
		this.detach();
	}
	public boolean check()
	{
		
		return false;
	}
	public String getpId() {
		return pId;
	}
	public void setpId(String pId) {
		this.pId = pId;
	}

}
